package com.group.controller;

import com.github.pagehelper.PageInfo;
import com.group.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.entity.Order;
import utils.BaseController;

import javax.servlet.http.HttpServletResponse;

@Api(tags = "用户账户")
@RestController
@RequestMapping(value = "/userAccount", produces = "application/json;charset=UTF-8")
public class UserAccountController extends BaseController {
    @Autowired
    private UserAccountService accountService;

    @ApiOperation("添加用户余额")
    @PostMapping(value = "/addUserBalanceByUserId.do", produces = "text/html;charset=UTF-8")
    public String addUserBalanceByUserId(Order order, HttpServletResponse response) {
        int result = accountService.addBalanceAndOrder(order);
        return writerSuccessResult(result, response);
    }

    @ApiOperation("查询用户余额")
    @GetMapping("/findUserBalanceByUserId.do")
    public String findUserBalanceByUserId(int userID, HttpServletResponse response) {
        double balance = accountService.findBalanceByUserId((long) userID);
        return writerSuccessResult(balance, response);
    }

    @ApiOperation("分页查询用户交易记录")
    @GetMapping(value = "/findPageByUserId.do")
    public String findPageByUserId(long userId, int currentPage, int pageSize, HttpServletResponse response) {
        PageInfo userRecordPage = accountService.findUserRecordPage(currentPage, pageSize, userId);
        return writerSuccessResult(userRecordPage, response);
    }

    @ApiOperation("根据用户id更新用户余额")
    @GetMapping("/updateUserBalance.do")
    public String updateUserBalance(long userId, double money) {
        int result = accountService.updateUserBalance(userId, money);
        return writerSuccessResult(result);
    }
}